package com.rq.base.ui.common;

import android.os.Bundle;


import com.rq.base.ui.BaseController;
import com.rq.base.ui.BaseFragment;

import java.io.Serializable;

import static com.rq.base.ui.BaseActivity.TAG_OPEN_CODE;
import static com.rq.base.ui.BaseActivity.TAG_OPEN_FOR;
import static com.rq.base.ui.BaseController.TAG_NAME;


public class CommonFragment extends BaseFragment {

    public CommonFragment() {//throws Exception
        //        String classFrom = new Exception().getStackTrace()[1].getClassName();//调用源 类名
        //        LOG.e("CommonFragment", "classFrom  = " + classFrom);
        //        LOG.e("CommonFragment", "CommonFragment.class  = " + CommonFragment.class.getName());
        //        if (!CommonFragment.class.getName().equals(classFrom)) {//只能本类中用
        //            LOG.utilLog("CommonFragment");
        //            throw new Exception("本项目中常规调用请使用 CommonFragment.get()");
        //        }
    }

    public static synchronized <C extends BaseController> CommonFragment instance(Class<C> clazz,
                                                                                  Bundle pass) {
        CommonFragment con = new CommonFragment();
        pass.putSerializable(TAG_NAME, clazz);
        con.setArguments(pass);
        return con;
    }

    public static synchronized <C extends BaseController> CommonFragment instance(Class<C> clazz,
                                                                                  Serializable... pass) {
        CommonFragment con = null;
        try {
            con = new CommonFragment();
            Bundle bundle = BaseController.getFraArguments(clazz, pass);
            con.setArguments(bundle);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return con;
    }

    public static synchronized <C extends BaseController> CommonFragment instanceWith(Class<C> clazz, int requestCode,
                                                                                      Serializable... pass) {
        CommonFragment con = null;
        try {
            con = new CommonFragment();
            Bundle bundle = BaseController.getFraArguments(clazz, pass);
            bundle.putBoolean(TAG_OPEN_FOR, true);
            bundle.putInt(TAG_OPEN_CODE, requestCode);
            con.setArguments(bundle);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return con;
    }

    @Override
    public BaseController getController(Object o, int i) {
        if (getArguments() == null || getArguments().getSerializable(TAG_NAME) == null) {
            return super.getController(o, i);
        }
        return super.getController((Class) getArguments().getSerializable(TAG_NAME));
    }
}